/**
 * list:[
 *  {
 *      url,//图片地址或视频封面
 *      video,//type为video是需要视频地址
 *      type://video|img 
 *  }
 * ]
 */
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        list:{
            type:Array,
            value:[],
        },
        id:{
            type:String,
            value:"myVideo",
        },
        width:{
            type:String,
            value:"355",
        },
        height:{
            type:String,
            value:"250",
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        playImg:true,
    },
    /**
     * 组件的方法列表
     */
    methods: {
        onSwitchToVideo(e) {
            const { index } = e.currentTarget.dataset;
            this.setData({
                playImg:false,
            },()=>{
                const videoContext = this.getVideoContext(index);
                videoContext.play();
            });
        },
    
        onVideoPause(e) {
            this.setData({
                playImg:true,
            })
        },
        
        onVideoStart(e) {
            console.log("播放")
            if(this.data.playImg) {
                const { index } = e.currentTarget.dataset;
                this.getVideoContext(index).pause();//ios 播放中页面跳转返回自动触发播放
            }
        },
    
        preview(e) {
            const { index } = e.currentTarget.dataset;
            const { list } = this.data;
            let urls = list.map(v=>v.url);
            wx.previewImage({
                current:urls[index],
                urls
            })
        },
        
        getVideoContext(i) {
            if(!this[`videoContext-${i}`]) {
                //组件中无法获取video示例 https://developers.weixin.qq.com/blogdetail?action=get_post_info&lang=zh_CN&token=&docid=000ac0760104209a0ae69a7fa56800
                //第二个参数传入this，真机正常，开发者工具不行
                this[`videoContext-${i}`] = wx.createVideoContext(`myVideo-${i}`,this);
            } 
            return this[`videoContext-${i}`];
        },
    }
})
